package com.springboot.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.springboot.dao.BookMapper;
import com.springboot.entity.Book;
import com.springboot.utils.ResultVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
@Slf4j
public class BookServiceImpl implements BookService{
    @Autowired
    private BookMapper bookMapper;
    @Override
    public ResultVO bookList(String name, int pageNum) {
        PageHelper.startPage(pageNum,5); //设置分页的页面容量
        Example example=new Example(Book.class);
        Example.Criteria criteria=example.createCriteria();
//模糊查询
        if(!StringUtils.isEmpty(name)){
//进行模糊查询
            criteria.andLike("name",'%'+name+'%');
        }
        List<Book> list = bookMapper.selectByExample(example);
        PageInfo<Book> pageInfo=new PageInfo<Book>(list);
        return new ResultVO(1005,"查询成功",pageInfo);
    }

    @Override
    public ResultVO addBook(Book book) {
        int i = bookMapper.insertSelective(book);  //选择性增加
        if(i>0){
            return new ResultVO(2000,"增加成功",i);
        }else{
            return new ResultVO(2001,"增加失败",null);
        }
    }

    @Override
    public ResultVO deleteBook(long bid) {
        Book book=new Book();
        book.setBid((int)bid);
        int n = bookMapper.delete(book);
        if(n>0){
            return new ResultVO(3000,"删除成功",n);
        }else{
            return new ResultVO(3001,"删除失败",null);
        }

    }

    @Override
    public ResultVO saveBook(Book book) {
        int i = bookMapper.updateByPrimaryKeySelective(book);
        if(i>0){
            return new ResultVO(3000,"修改成功",i);
        }else{
            return new ResultVO(3001,"修改失败",null);
        }
    }


}

